/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
//working copy at 10.25am
package Chat;

/**
 *
 * @author lubinchen
 */



import java.net.InetAddress;
import java.util.*;




public class IpChat_Constructor {

   public static int port=4001;
   public ArrayList<Hashtable> received_ht_al=new ArrayList<Hashtable>(0);
   public ArrayList<Boolean> clientconnected=null;
   private InetAddress tempIpAdd=null;
   private GUI_chat gui=null;
   public static Boolean have_new_text=false;
   public IpChat_Main mymain=null;
   public static String received_new_text;
   public ChatServer server=null;
   public static ChatClient client=null;

   //public static ChatClient clients[] = new ChatClient[20];

   public static ArrayList<ChatClient> clients =new ArrayList<ChatClient>(0) ;


   public static Integer clientCount=0;
   private static client.ClientView _clientView = null;




   public IpChat_Constructor(ArrayList<Hashtable> ht, IpChat_Main newmain, client.ClientView clientView){


        this._clientView = clientView;
        System.out.println("\n");
        System.out.println("\n");
        System.out.println("\n");
        System.out.println("@IpChat_Constructor's start of program. New object of IpChat_Constructor created");
        System.out.println("\n");
        System.out.println("\n");
        System.out.println("\n");

        server=new ChatServer(port, this);
        mymain= newmain;

        if(ht.size()>0){
            update_ht(ht);
            checkunconnected();
       }
   
   }


   //ok 
   public void update_ht(ArrayList<Hashtable> New_ht){



        System.out.println("\n");
        System.out.println("@IpChat_Constructor.UUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUUU update_ht() is called ! ");
        System.out.println("\n");

       // create new client info
       for (int hti = 0; hti < New_ht.size(); hti++)
       {
               Boolean existAlready=false;

      
               for(int i=0; i<received_ht_al.size() ;i++ )
                   if ((received_ht_al.get(i).containsValue( New_ht.get(hti).get("ip")) ))
                   {
                        existAlready = true;
                        break;
                   }
         
               if(existAlready==false)
               {
                    received_ht_al.add(New_ht.get(hti));
                    System.out.println("~~~~~~@Ip_Constructor.update_ht:  New Client copied from Hashtable updated to ~received_ht_al~= "+New_ht.get(hti));
                    Boolean notconnected=false;
                    Hashtable temh=received_ht_al.get(received_ht_al.size()-1);
                    temh.put("connected", notconnected);
               }

       }

       checkunconnected();

      // update team info if neccessary
       for (int hti = 0; hti < New_ht.size(); hti++)
       {
               for(int i=0; i<received_ht_al.size() ;i++ )
               {
                 if (received_ht_al.get(i).get("ip").equals(New_ht.get(hti).get("ip")))
                    if (received_ht_al.get(i).get("team")!=New_ht.get(hti).get("team") )
                    {
                        received_ht_al.get(i).remove("team");
                        Integer newT= (Integer)New_ht.get(hti).get("team");
                        received_ht_al.get(i).put("team", newT);
                    }   
                 
                 if (server.MyOwnIP.equals(New_ht.get(hti).get("ip"))){
                     ChatServer.ownTeamNumber=(Integer)New_ht.get(hti).get("team");
                      System.out.println("\n");
                      System.out.println("@IpChat_Constructor.update_ht. ownTeamNumber has been updated to "+ChatServer.ownTeamNumber);
                      System.out.println("\n");
                   }
           }

       }


        checkunconnected();

       System.out.println("\n");
       System.out.println("@IpChat_Constructor. @Lastline of: update_ht. After HT Update !   After HT Update !   After HT Update !  After HT Update !  received_ht_al updated as.. ");
          for (int p=0;p<received_ht_al.size();p++)
            System.out.println("Line "+p+": "+received_ht_al.get(p).toString());
       System.out.println();

       checkunconnected();

   }




   private void ConnectClients(){

        for (int i=0; i< received_ht_al.size();i++)
            if(  ( (Boolean)received_ht_al.get(i).get("connected")  )==false )
            {
              InetAddress Inet= getInet( (String)  received_ht_al.get(i).get("ip") );
              Integer team=(Integer)received_ht_al.get(i).get("team");

              System.out.println("!!!!!!~~~~~~~~~~~~~~@IPChat_Constructor.Trying to connect to  ip= "+(String)received_ht_al.get(i).get("ip"));
              System.out.println("!!!!!!~~~~~~~~~~~~~~@IPChat_Constructor.this client's teamNumber= "+received_ht_al.get(i).get("team"));
              ChatClient newclient = new ChatClient( Inet, port, (String)received_ht_al.get(i).get("username"), this,team );
              clients.add(newclient);
            }




       System.out.println("\n");
       System.out.println("@IpChat_Constructor. @ConnectClients() !  received_ht_al updated as.. ");
          for (int p=0;p<received_ht_al.size();p++)
            System.out.println("Line "+p+": "+received_ht_al.get(p).toString());
       System.out.println();
        
        checkunconnected();
        checkunconnected();
   }

   public void connection_successful(String clientsIP){


       for(int i=0; i<received_ht_al.size(); i++)
           if(received_ht_al.get(i).get("ip")==null? clientsIP==null : received_ht_al.get(i).get("ip").equals(clientsIP))
           {
               received_ht_al.get(i).remove("connected");
               Boolean connected=true;
               received_ht_al.get(i).put("connected", connected);
               //System.out.println("received_ht_al.get(i).get(username)= "+received_ht_al.get(i).get("username")+".get(connected)= "+ received_ht_al.get(i).get("connected"));

           }



       System.out.println("\n");
       System.out.println("@IpChat_Constructor. @connection_successful() !  received_ht_al updated as.. ");
          for (int p=0;p<received_ht_al.size();p++)
            System.out.println("Line "+p+": "+received_ht_al.get(p).toString());
       System.out.println();


   }



   public void checkunconnected(){

       System.out.println("@IpChat_Constructor. called:     checkunconnected() checkunconnected()  checkunconnected()   checkunconnected()  checkunconnected()  checkunconnected()   checkunconnected()   checkunconnected() ! ");

       //print out the entire client and server threads
       System.out.println("\n");
       System.out.println("\n");
       System.out.println(".........Checking running threads: Client threads................Current Client threads:");
       for (int i=0; i<clients.size();i++)
           System.out.println(clients.get(i).clientname);

      /* System.out.println("\n");
       System.out.println(".........Checking running threads: Server threads................Current Server threads:");
       for (int i=0; i<ChatServer.clients.size();i++)
           System.out.println(ChatServer.clients.get(i).name);
       System.out.println("\n");
       System.out.println("\n");*/

       for (int i=0; i< received_ht_al.size();i++)
            if(( (Boolean)received_ht_al.get(i).get("connected") )==false)
            {
                 
                 System.out.println("@IpConstructor.checkUnconnected(). Found an uncnncected client of name "+received_ht_al.get(i).get("username"));
                 ConnectClients();

           }
       

       System.out.println("\n");
       System.out.println("@IpChat_Constructor. @checkUnconnected() !  received_ht_al updated as.. ");
          for (int p=0;p<received_ht_al.size();p++)
            System.out.println("Line "+p+": "+received_ht_al.get(p).toString());
       System.out.println();


        //checkunconnected();

   }


    public InetAddress getInet(String DestinationIP)
    {

        try{
            tempIpAdd= InetAddress.getByName(DestinationIP);

            }catch (Exception e){}

        return tempIpAdd;
    }


}

